package com.bw.yk06;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

public class test2 {
    public static void main(String[] args) throws Exception {
        //流式环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度为1
        env.setParallelism(1);
        //kafka读取数据
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "hadoop-single:9092");
        properties.setProperty("group.id", "test2");
        DataStream<String> stream = env
                .addSource(new FlinkKafkaConsumer<>("tms_ods", new SimpleStringSchema(), properties));

        //cdc
        DebeziumSourceFunction<String> build = MySQLSource.<String>builder()
                .hostname("hadoop-single")
                .port(3306)
                .databaseList("tms01") // monitor all tables under inventory database
                .tableList("tms_config_dim")
                .username("root")
                .password("root")
                .startupOptions(StartupOptions.initial())
                .deserializer(new MyDeserializationSchemaFunction()) // converts SourceRecord to String
                .build();
        //广播流
        MapStateDescriptor<String, String> mapStateDescriptor = new MapStateDescriptor<>("tests", String.class, String.class);
        //生成流
        DataStreamSource<String> streamSource = env.addSource(build);
        //streamSource.print();
        BroadcastStream<String> broadcast = streamSource.broadcast(mapStateDescriptor);
        //主流与配置流 connnect
        BroadcastConnectedStream<String, String> connect = stream.connect(broadcast);
        //使用方法
        SingleOutputStreamOperator<String> process = connect.process(new BrodcastProcess(mapStateDescriptor));
        process.print();

        //执行
        env.execute();
    }
}
